package com.example.shop.fgo.task.upload.view;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.example.shop.fgo.R;
import com.example.shop.fgo.base.YBaseActivity;
import com.example.shop.fgo.task.common.controller.TaskMsg;
import com.example.shop.fgo.task.common.controller.UpdatePart;
import com.example.shop.fgo.task.common.view.TaskingsActivity;
import com.example.shop.fgo.task.upload.controller.GetSDEntityUtil;
import com.example.shop.fgo.task.upload.controller.UploadController;
import com.example.shop.fgo.task.upload.model.SDEntity;
import com.example.shop.fgo.task.upload.model.UploadTask;
import com.orhanobut.logger.Logger;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

/*
    上传：SDEntity---上传谁   cid---上传到哪儿
 */
public class SDScanActivity extends YBaseActivity {
    private ListView mLvSdScanActivity;
    private Button mBtUpload;
    private List<SDEntity> mSDEntities;
    private ItemLvSdScanAdapter mItemLvSdScanAdapter;
    private String mCidNow;
    private UploadController mUploadController=new UploadController();
    @Override
    public int setRootView() {
        return R.layout.activity_sdscan;
    }

    @Override
    public void initViews() {
        this.setTitleCenter("选择上传文件");
        mLvSdScanActivity = (ListView) findViewById(R.id.lv_sd_scan_activity);
        mBtUpload = (Button) findViewById(R.id.bt_upload);
        mLvSdScanActivity.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                SDEntity sdEntity = mSDEntities.get(position);

                if (sdEntity.isDir()) {

                    Toast.makeText(mActivitySelf, "打开这个文件夹 "+sdEntity.getName(), Toast.LENGTH_SHORT).show();
                    mSDEntities=GetSDEntityUtil.getDir(sdEntity.getAbsolutePath());
                    mItemLvSdScanAdapter.setEntities(mSDEntities);
                }else {
                    Toast.makeText(mActivitySelf, "这是个文件，先不管 "+sdEntity.getName(), Toast.LENGTH_SHORT).show();
                }
            }
        });

        mBtUpload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mUploadController.addTasksToQueue(mItemLvSdScanAdapter.mEntitiesChecked,mCidNow);

            }
        });
    }


    @Override
    protected void onResume() {
        super.onResume();
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void receive(final TaskMsg taskMsg){
        if (taskMsg.mUpdatePart!= UpdatePart.EXISTS){
            return;
        }

        if (taskMsg.exists) {
            AlertDialog.Builder builder=new AlertDialog.Builder(this)
                    .setTitle("这个存在了！")
                    .setMessage(taskMsg.mTask.getName()+"  已存在，请问你是不是要覆盖他？？？")
                    .setNegativeButton("不", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Logger.e("接收到了消息  ："+taskMsg.mTask);
                            UploadTask uploadTask= (UploadTask) taskMsg.mTask;
                            uploadTask.decision=UploadTask.BUFG;
                            mUploadController.cancelTask((UploadTask)taskMsg.mTask );
                            taskMsg.recycle();
                        }
                    })
                    .setPositiveButton("覆盖他", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            UploadTask uploadTask= (UploadTask) taskMsg.mTask;
                            uploadTask.decision=UploadTask.FG;
                            goToActivity(TaskingsActivity.class);
                            taskMsg.recycle();
                        }
                    })
                    .setCancelable(false);
            builder.create().show();

        }else{
            goToActivity(TaskingsActivity.class);
            taskMsg.recycle();
        }



    }
    @Override
    public void initDatas() {
        mSDEntities= GetSDEntityUtil.getRootSD();
        mItemLvSdScanAdapter = new ItemLvSdScanAdapter(this,mSDEntities);

        mLvSdScanActivity.setAdapter(mItemLvSdScanAdapter);
        mCidNow=this.getIntent().getExtras().getString("cid");
    }
}
